package com.hengxinguotong.zhihuichengjian.ui.lift.warning;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.LinearLayout;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.DeviceListPagerAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.customtimepicker.TimePickerActivity;
import com.hengxinguotong.zhihuichengjian.customtimepicker.bean.DayTimeEntity;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.ui.lift.OnFragmentInteractionListener;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/2/20
 *
 * 升降机预警
 */

public class LiftWarningListActivity extends BaseActivity implements View.OnClickListener, OnFragmentInteractionListener {
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.search_date_tv)
    HXTextView searchDateTv;
    @BindView(R.id.magic_indicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    private List<BaseFragment> fragmentList;
    private DeviceListPagerAdapter deviceListPagerAdapter;
    private List<String> mTitleDataList;
    private CommonNavigatorAdapter navigatorAdapter;
    private String startTime, endTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lift_warning_list);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        titleTv.setText("升降机预警");

        startTime = dateFormat(new Date());
        endTime = dateFormat(new Date());
        searchDateTv.setText(startTime + " 至 " + endTime);

        viewPager.setOffscreenPageLimit(3);
        fragmentList = new ArrayList<BaseFragment>();
        LiftWarningListFragment yujingFragment = LiftWarningListFragment.newInstance(0, startTime, endTime);
        yujingFragment.setActivity(this);
        fragmentList.add(yujingFragment);
        LiftWarningListFragment baojingFragment = LiftWarningListFragment.newInstance(1, startTime, endTime);
        baojingFragment.setActivity(this);
        fragmentList.add(baojingFragment);
        LiftWarningListFragment quanbuFragment = LiftWarningListFragment.newInstance(2, startTime, endTime);
        quanbuFragment.setActivity(this);
        fragmentList.add(quanbuFragment);
        deviceListPagerAdapter = new DeviceListPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(deviceListPagerAdapter);


        mTitleDataList = new ArrayList<String>();
        mTitleDataList.add("预警");
        mTitleDataList.add("报警");
        mTitleDataList.add("全部");

        CommonNavigator commonNavigator = new CommonNavigator(this);
        navigatorAdapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitleDataList == null ? 0 : mTitleDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mTitleDataList.get(index));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_666666));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_f288ae5));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.color_f288ae5));
                return indicator;
            }
        };
        commonNavigator.setAdapter(navigatorAdapter);
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return Utils.dip2px(LiftWarningListActivity.this, 40);
            }
        });

        final FragmentContainerHelper fragmentContainerHelper = new FragmentContainerHelper(magicIndicator);
        fragmentContainerHelper.setInterpolator(new OvershootInterpolator(2.0f));
        fragmentContainerHelper.setDuration(300);
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                fragmentContainerHelper.handlePageSelected(position);
            }
        });
    }

    @OnClick({R.id.back_tv, R.id.search_date_tv})
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_tv:
                finish();
                break;
            case R.id.search_date_tv:
                Intent intent = new Intent(this, TimePickerActivity.class);
                startActivityForResult(intent, 0);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            if(requestCode == 0 && data != null){
                DayTimeEntity startDay = (DayTimeEntity) data.getSerializableExtra("startDay");
                DayTimeEntity stopDay = (DayTimeEntity) data.getSerializableExtra("stopDay");
                processDate(startDay, stopDay);
            }
        }
    }

    private void processDate(DayTimeEntity startDay, DayTimeEntity stopDay){
        startTime = dateFormat(startDay);
        endTime = dateFormat(stopDay);
        searchDateTv.setText(startTime  + " 至 " + endTime);
        for (BaseFragment fragment:fragmentList){
            ((LiftWarningListFragment)fragment).OnActivityInteraction(startTime, endTime);
        }
    }

    private String dateFormat(DayTimeEntity dayTimeEntity){
        StringBuffer dateSb = new StringBuffer();
        dateSb.append(dayTimeEntity.getYear() + "-");
        if(dayTimeEntity.getMonth() > 9){
            dateSb.append(dayTimeEntity.getMonth() + "-");
        }else {
            dateSb.append( "0" + dayTimeEntity.getMonth() + "-");
        }
        if(dayTimeEntity.getDay() > 9){
            dateSb.append(dayTimeEntity.getDay());
        }else {
            dateSb.append( "0" + dayTimeEntity.getDay());
        }
        return dateSb.toString();
    }

    private String dateFormat(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    @Override
    public void onFragmentInteraction(int index, int count) {
        //获取数量
        String title;
        switch (index){
            case 0:
                title = "预警" + "(" + count + ")";
                mTitleDataList.set(index, title);
                break;
            case 1:
                title = "报警" + "(" + count + ")";
                mTitleDataList.set(index, title);
                break;
        }
        navigatorAdapter.notifyDataSetChanged();
    }

    @Override
    public void onUpdateLift(String heightPercent) {

    }
}
